package com.hpdz.hotel_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpdz.hotel_back.entity.Customer;
import com.hpdz.hotel_back.entity.Order;
import com.hpdz.hotel_back.mapper.CustomerMapper;
import com.hpdz.hotel_back.mapper.OrderMapper;
import com.hpdz.hotel_back.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
@Transactional
@Slf4j
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService{
    @Resource
    private OrderMapper orderMapper;

    @Override
    public Customer login(Customer customer) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq(true,"email",customer.getEmail());
        wrapper.eq(true,"passwd",customer.getPassword());
        return getOne(wrapper);
    }

    @Override
    public IPage<Order> selectByPage(Integer current, Integer size, HttpSession session) {

        Customer cur = (Customer)session.getAttribute("cur_cust");
        if(cur == null) return null;

        Page<Order> orderPage = new Page<>(current,size);

        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();

        orderQueryWrapper.eq(true,"cust_id",cur.getId());

        return orderMapper.selectPage(orderPage, orderQueryWrapper);

    }
}
